﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataReceiver
{
    public partial class FormOptions : Form
    {
        public FormOptions()
        {
            InitializeComponent();
        }

        private void FormOptions_Load(object sender, EventArgs e)
        {
            LoadSettings();
        }

        private void LoadSettings(bool load = true)
        {
            if (load)
            {
                textBoxTargetDir.Text = Properties.Settings.Default.TargetDir;
                textBoxFilePathFormatString.Text = Properties.Settings.Default.FilePathFormatString;
            }
            else
            {
                Properties.Settings.Default.TargetDir = textBoxTargetDir.Text;
                Properties.Settings.Default.FilePathFormatString = textBoxFilePathFormatString.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            LoadSettings(false);
            Properties.Settings.Default.Save();
            DialogResult = DialogResult.OK;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var browser = new FolderBrowserDialog();
            browser.SelectedPath = Properties.Settings.Default.TargetDir;
            var res = browser.ShowDialog(this);
            if (res == DialogResult.OK)
            {
                textBoxTargetDir.Text = browser.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var lprCaptured = new Kise.LprCapturer.LprCaptured();
            lprCaptured.CaptureTime = DateTime.Now;
            lprCaptured.LocationId = "监控点id";
            lprCaptured.LprColorCode = 0;
            lprCaptured.LprNumber = "车牌号码";
            lprCaptured.RoadNo = 0;
            lprCaptured.Speed = 50;
            lprCaptured.SpeedLimit = 60;
            lprCaptured.ViolationCode = 3;

            try
            {
                string path = Kise.LprCapturer.Helper.GeneratePath(
                textBoxFilePathFormatString.Text,
                textBoxTargetDir.Text,
                lprCaptured, 0);

                string dir = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                File.WriteAllBytes(path, new byte[0]);

                textBoxResult.Text = path;
            }
            catch (Exception ex)
            {
                MessageBox.Show("生成路径发生错误：\r\n" + ex.Message);
            }

        }
    }
}
